package com.allwees.bs.module.pms.dao;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.allwees.bs.module.pms.entity.Category;
import com.allwees.core.dao.mybatis.dao.BaseDao;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.annotations.Update;
import org.springframework.util.CollectionUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * @author yangylsky
 * @since 2019-08-21 09:26
 */
public interface CategoryDao extends BaseDao<Category> {

    @Update({"UPDATE prod_category set product_seq=#{seq} WHERE id=#{id}"})
    void updateSeq(long id, String seq);

    default Map<String, Category> selectByNos(Set<String> nos) {
        if (CollectionUtils.isEmpty(nos)) {
            return new HashMap<>(0);
        }

        QueryWrapper<Category> wrapper = new QueryWrapper<>();
        wrapper.in("no", nos);

        List<Category> list = selectList(wrapper);
        return list.stream().collect(Collectors.toMap(Category::getNo, o -> o));
    }

    default Category selectByNo(String no) {
        if (StringUtils.isEmpty(no)) {
            return null;
        }

        QueryWrapper<Category> wrapper = new QueryWrapper<>();
        wrapper.eq("no", no);
        return selectOne(wrapper);
    }

    default List<Category> findAll() {
        QueryWrapper<Category> wrapper = new QueryWrapper<>();
        wrapper.eq("status", 0);
        return selectList(wrapper);
    }
}
